<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        width="100"
        label="新闻ID"
        prop="nid">
      </el-table-column>
      <el-table-column
        label="新闻发布者"
        prop="uid">
      </el-table-column>
      <el-table-column
        label="新闻标题"
        prop="title">
      </el-table-column>
      <el-table-column
        label="新闻发布时间"
        prop="created_time">
      </el-table-column>
      <el-table-column
        label="新闻描述"
        prop="description">
      </el-table-column>
      <el-table-column
        label="新闻类型"
        prop="type">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            :disabled="scope.row.step === '已审核'"
            @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>


    <el-pagination
      background
      layout="prev, pager, next"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      @current-change=getAllArticle>
    </el-pagination>

  </div>
</template>

<script>
export default {
  name: "ArticleManagement",
  inject: ['reload'],

  data() {
    return {
      tableData: [],
      currentPage: 1,
      total: 0,
      pageSize: 10,
    }
  },

  methods: {
    handleDelete() {

    },

    handleData(item) {
      if (item.created_time != null) {
        var date = new Date(item.created_time);
        var formatted_time = `${date.getMonth() + 1}月${date.getDate()}日${date.getHours()}时${date.getMinutes()}分`;
        item.created_time = formatted_time
      }

      if (item.description != null) {
        item.description = item.description.substring(0, 15) + "..."
      }
    },

    getAllArticle( currentPage) {


      // console.log(currentPage)

      this.axios({
        method: "get",
        url: "/news/" + 'national_news' + "/" + currentPage,
      }).then(resp => {
        if (resp) {
          if (resp.data.code == 200) {
            // console.log('请求成功')
            // console.log(resp.data.data)
            this.tableData = resp.data.data.records

            // console.log(this.tableData[0])
            for (var item of this.tableData) {
              this.handleData(item)
            }

            this.currentPage = resp.data.data.current
            this.total = resp.data.data.total
            this.pageSize = resp.data.data.size

            return this.tableData

          }
        }
      })
    },
  },


  created() {

    this.getAllArticle( 1)
  },


}
</script>

<style scoped>

</style>
